﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CameraParentClassLibrary;//引入相机父类
using CameraFactoryClassLibrary;//引用反射工厂类库

namespace _00_反射_简易框架
{
    public partial class Form1 : Form
    {

        CameraParent cameraParent = null;

        public Form1()
        {
            InitializeComponent();

        }
        /// <summary>
        /// 窗体显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Shown(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (System.IO.File.Exists("CameraInfo.txt"))
            {
                //读取全部的内容然后显示在控件上
                string[] ReadInfoArray = System.IO.File.ReadAllLines("CameraInfo.txt");
                this.textBox1.Text = ReadInfoArray[0];
                
                
                this.textBox3.Text = ReadInfoArray[3];
                this.textBox4.Text = ReadInfoArray[4];
                this.trackBar1.Value = Convert.ToInt32(ReadInfoArray[5]);
                this.label7.Text = this.trackBar1.Value.ToString();                
                
                this.textBox2.Text = ReadInfoArray[1];
                if (Convert.ToBoolean(ReadInfoArray[2]))
                {
                    //程序启动后立即运行
                    this.radioButton1.Checked = true;
                    //通过反射获取相机对象
                    cameraParent = CameraFactory.GetCameraObj();
                    cameraParent.DelayTime = this.trackBar1.Value;

                    CameraControl cameraControl = new CameraControl(cameraParent);
                    cameraControl.Dock = DockStyle.Fill;
                    cameraControl.SetCameraName(this.textBox3.Text);

                    this.panel1.Controls.Clear();
                    this.panel1.Controls.Add(cameraControl);

                    //设置相机启动
                    cameraParent.OpenCamera();
                    //设置为连续采集
                    cameraParent.CollectOneContinuous();

                }
                else
                {
                    this.radioButton2.Checked = true;
                }

            }
            this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);

        }
        /// <summary>
        /// 设置按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //保存界面配置
            SaveControlValue();

            //通过反射获取相机对象
            cameraParent = CameraFactory.GetCameraObj();
            cameraParent.DelayTime = this.trackBar1.Value;

            CameraControl cameraControl = new CameraControl(cameraParent);
            cameraControl.Dock = DockStyle.Fill;
            cameraControl.SetCameraName(this.textBox3.Text);
            
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(cameraControl);
            

        }
        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            SaveControlValue();
        }

        public void SaveControlValue()
        {
            string[] SaveInfoArray = new string[6];
            //动态链接库的位置
            SaveInfoArray[0] = this.textBox1.Text;
            //加载类的名称
            SaveInfoArray[1] = this.textBox2.Text;
            //是否立即加载
            SaveInfoArray[2] = this.radioButton1.Checked.ToString();
            //相机名称
            SaveInfoArray[3] = this.textBox3.Text;
            //图片路径
            SaveInfoArray[4] = this.textBox4.Text;
            //延时时间
            SaveInfoArray[5] = this.trackBar1.Value.ToString();

            System.IO.File.WriteAllLines("CameraInfo.txt", SaveInfoArray);
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (cameraParent!=null)
            {
                cameraParent.DelayTime = this.trackBar1.Value;
            }
             this.label7.Text = this.trackBar1.Value + "MS";
        }
    }
}
